<?php

/*™*******************************************************************************************™*/

function inputSafe($elementVariable) {

	$elementVariable = htmlspecialchars(trim($elementVariable), ENT_QUOTES);
	
	return $elementVariable;

}

/*™*******************************************************************************************™*/

function validateElementAdminAccount($ruleElement, $index) {

	/* initialized variables */
	$errorFieldName = "";
	$errorMessage = "";
	$errorMessageJSON = "";
	$errorFlag = FALSE;
	$value = array();
	$systemErrors = array();
	$customErrors = array();

	/* create validate object */
	$objValidator = new Validate();

	/* validate element */

	foreach ($ruleElement as $elementName => $elementRule) {
		foreach ($elementRule[$index] as $rule => $value) {
			switch ($rule) {
				case "checkNotEmpty":
					$objValidator->checkNotEmpty($elementName, $index, $value . " - Please enter a value");
                    break;

				case "checkNotSelect":
					$objValidator->checkNotSelect($elementName, $index, $value . " - Please select a value");
                    break;

				case "checkIsAlphabetic":
					$objValidator->checkIsAlphabetic($elementName, $index, $value . " - Please enter a value letters only");
                    break;

				case "checkIsDigit":
					$objValidator->checkIsDigit($elementName, $index, $value . " - Please enter a value digit only");
                    break;

				case "checkIsCurrency":
					$objValidator->checkIsCurrency($elementName, $index, $value . " - Please enter a value digit or . only");
                    break;

				case "checkLength":
					$splitValue = explode(":", $value);
					$objValidator->checkLength($elementName, $splitValue[0], $splitValue[1], $index, $splitValue[2] . " - Please enter a value between " . $splitValue[0] . " - " . $splitValue[1] . " characters");
                    break;

				case "checkIsValidEmail":
					$objValidator->checkIsValidEmail($elementName, $index, $value . " - Please enter a valid email address");
                    break;

				case "checkIsTelephone":
					$objValidator->checkIsTelephone($elementName, $index, $value . " - Please enter a valid telephone");
                    break;

				case "checkIdentical":
					$splitValue = explode(":", $value);
					$objValidator->checkIdentical($elementName, $splitValue[0], $index, $splitValue[1] . " - Does not match with password");
                    break;

				case "checkIdenticalWithVariable":
					$splitValue = explode(":", $value);
					$objValidator->checkIdenticalWithVariable($elementName, $splitValue[0], $index, $splitValue[1] . " - Does not match");		
                    break;
                    
					$index = $index + 1;
				
			}
		}
	}

	/* show system error message if validate fail */
	$systemErrors = $objValidator->getErrors();

	if (!empty($systemErrors)) {
		foreach ($systemErrors as $fieldName => $systemInfo) {
			foreach ($systemInfo[0] as $systemElement) {
				$errorFieldName = $fieldName; // return field name for focus error field
				$errorFlag = TRUE;
				break;
			}
			break;
		}
	}

	/* show custom error message if validate fail */
	$customErrors = $objValidator->getMyErrors();

	if (!empty($customErrors)) {
		foreach ($customErrors as $customError) {
			$errorMessage = $customError;
			$errorFlag = TRUE;
			break;
		}
	}

	/* return error message if found */
	if ($errorFlag === TRUE) {
		/* AJAX return JSON message */
		return $errorMessageJSON = "{'" . $errorFieldName . "':'" . $errorMessage . "'}";
	}

}

/*™*******************************************************************************************™*/

/* generate page in web page use with class paging */
function generatePaging($recordCount, $recordPerPage, $type) {

	$strParamPage = "page"; // name of url parameter that stores actual page no.
	$page = (isset($_GET[$strParamPage]) && !empty($_GET[$strParamPage])) ? $_GET[$strParamPage] : 1; // set the actual page no.
	
	/* create paging object */
	$objPaging = new paging_output($page, $recordCount);
	$objPaging->setRecordsPerPage($recordPerPage);

	/* get page information */
	$first = 1;
	$last = $objPaging->getLast();
	$prev = $objPaging->getPrev();
	$next = $objPaging->getNext();
	$limit1 = $objPaging->getLimit1();
	$limit2 = $objPaging->getLimit2();
	$pages_total = $objPaging->getPagesTotal();

	/* names of the links default values are: first, last, prev, next, page */
	$objPaging->setNames("<img src='./images/bt/bt_first.jpg' alt='First' border='0' align='absmiddle' />", 
	                     "<img src='./images/bt/bt_last.jpg' alt='Last' border='0' align='absmiddle' />", 
						 "<img src='./images/bt/bt_previous.jpg' alt='Previous' border='0' align='absmiddle' />", 
						 "<img src='./images/bt/bt_next.jpg' alt='Next' border='0' align='absmiddle' />", 
						 "page");

	/* optional: add some additional GET param/value pairs */
	if ($type == "BACKEND") {
		$getParams = array("token" => $_SESSION["token"]);
		$objPaging->setGetParams($getParams);
	}

	return array($objPaging, $limit1, $limit2);
	
}

/*™*******************************************************************************************™*/

/* shift string and rotate (SAP format)*/
function shiftLeftAndRoate($string) {
	
	$firstChar = "";
	$result = "";
	
	$string = number_format($string, 2);
	$firstChar = substr($string, 0, 1);
	$result = substr($string, 1) . $firstChar;

 	return $result;

}

/*™*******************************************************************************************™*/

?>
